package com.bluesky.plum.uimodels.render.swing.components;

import java.awt.Dimension;

import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.bluesky.plum.richdomain.RichDomainField;
import com.bluesky.plum.uimodels.standard.components.EventListener;
import com.bluesky.plum.uimodels.standard.components.TextBox;

/**
 * the core domain model is POJO. the rich domain object can be converted to and
 * from the POJO model. So, we want the UI change will automatically update the
 * POJO model through the rich domain model.
 * 
 * @author HZ00260
 * 
 */
public class STextBox extends TextBox {

	private JTextField textField;

	public STextBox(RichDomainField field) {
		super(field);

		textField = new JTextField((String) getField().value);
		//textField.setColumns(15);
		textField.setPreferredSize(new Dimension(60,20));

		textField.setToolTipText(field.description);
	}

	@Override
	public Object getNativeComponent() {

		return textField;
	}

	@Override
	public String getText() {

		return textField.getText();

	}

	@Override
	public void setText(String text) {

		textField.setText(text);
	}

	@Override
	public void setValueChangeEventListener(
			EventListener valueChangeEventListener) {

		super.setValueChangeEventListener(valueChangeEventListener);

		textField.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void changedUpdate(DocumentEvent e) {
				getValueChangeEventListener().process();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				getValueChangeEventListener().process();

			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				getValueChangeEventListener().process();

			}
		});
	}

}
